package Document;

use strict;

use XML::Simple;
#use File::Slurp;

sub parse_XML {
  my $file=shift; # input file name
  # create an object
  my $xml = new XML::Simple;
  # read XML file
  my $data = $xml->XMLin($file);
  return $data;
}

# convert input hash of contents to plain txt file
# could be modified to adjust another type of input doc
sub convert_to_generic {
  my $hash=shift; # reference of hash
  my $dest_dir=shift; #path to the output dir
  
  # set a field to empty string if it's missed in the original file
  if (!defined $hash->{"TITLE"}) {
    $hash->{"TITLE"} = "";
  }
  if (!defined $hash->{"AUTHOR"}) {
    $hash->{"AUTHOR"} = "";
  }
  if (!defined $hash->{"TEXT"}) {
    $hash->{"TEXT"} = "";
  }
  $hash->{"TITLE"} =~ s/^\n//g;
  chomp $hash->{"TITLE"};
  $hash->{"AUTHOR"} =~ s/^\n//g;
  chomp $hash->{"AUTHOR"};

  # write the plain text content into doc_id.txt
  my $text = $hash->{"TITLE"}."\n\n".$hash->{"AUTHOR"}."\n"
           . $hash->{"TEXT"};
  my $docno = $hash->{"DOCNO"};
  $docno =~ s/[^0-9]*([0-9]+)[^0-9]*/$1/g;
  open FILE, ">$dest_dir/$docno.txt" or die "Could not create file \"$docno.txt\"\n";
  print FILE $text;
  close FILE;
}

#pick snippets that contain the query terms
sub summary {
  my ($docId, $position) = @_;
  my $doc_path = 'txt_corpus';
  my $text = slurp_file("$doc_path/$docId.txt");
  $text =~ s/\s\./\./g;
  my $return = "";
  my $start = 0;
  my @words = split /\s+/, $text;
  if ($position > 11){
      $start = $position - 11;
  }

  for(my $count = $start; $count <= ($start+20); $count++){
      if (exists $words[$count]) {
        $return .= "$words[$count] ";
      }
      if ($count == $start +10) {
        $return .= "\n\t\t\t\t";
      }
  }
  return $return;
}

# read a txt file into a single string
sub slurp_file {
  my $filename = shift;
  local($/, *FH);
  open( FH, $filename) || die "Error: Could not open $filename for snippet\n";
  my $text = <FH>;
  return $text;
}
 
sub find_tf {
  my $input = shift;
  chomp $input;
  my ($docid, $term) = split / /, $input;
  print "$docid, $term\n";

  my $text = slurp_file "txt_corpus/$docid.txt";
  my $count = 0;  
  while ( $text =~ s/$term// ) {
    $count++;
  }

  print "The freq of term \"$term\" in doc #$docid is $count\n";
}

1;

